package rhinestone.Model;

import java.util.HashMap;
import java.io.Serializable;

/**
 * The container of all Products.
 *
 * @author Group 2
 * @version 2010.12.16
 */
public class ProductContainer implements Serializable {
	private HashMap<String, Product> products;
	private static ProductContainer instance;
	private boolean changed;

	/**
	 * Private constructor for instance variable initialization.
	 */
	private ProductContainer() {
		products = new HashMap<String, Product>();
		changed = false;
	}

	/**
	 * Singleton pattern.
	 * @return The object of this container.
	 */
	public static ProductContainer getInstance() {
		if(instance == null)
			instance = new ProductContainer();
		return instance;
	}

	/**
	 * Adds product to the database.
	 * Also it changes the status of container as changed,
	 * so before exiting the program, this container will be serialized.
	 *
	 * @param product Product to be added.
	 */
	public void addProduct(Product product) {
		products.put(product.getBarcode(), product);
		changed = true;
	}

	/**
	 * Looks for a product in the database with the provided bar code.
	 * @param The bar code of the product.
	 * @return The product object (null if not found).
	 */
	public Product getProduct(String barcode) {
		return products.get(barcode);
	}

	/**
	 * Removes product with provided bar code.
	 * Also it changes the status of container as changed, 
	 * so before exiting the program, this container will be serialized.
	 *
	 * @param barcode The bar code of the product to remove.
	 * @return The removed product.
	 */
	public Product removeProduct(String barcode) {
		changed = true;
		return products.remove(barcode);
	}

	/**
	 * Self talking methods. Changed setter and getter.
	 */
	public boolean isChanged() {
		return changed;
	}
	public void setChanged(boolean changed) {
		this.changed = changed;
	}
}
